<?php
	namespace Home\Model;
	use Think\Model;

	class TDownloadModel extends Model
	{
	 	protected $trueTableName = "t_download";

	 	public function get_movie($program_id,$code_type)
	 	{	 
	 		$res = $this->get_movie_info($program_id,$code_type);
	 		// dump($res);exit();
	        $code_type = C('CODERATE');
	        $ftp = "ftp://tianjin_iptv:7IJ4pkqlRC@47.98.244.226:2121/";
	 		$res = array_map(function($movie) use($code_type,$ftp){
	 			return array_merge($movie,[
	 				'cp_file_url' => $ftp.$movie['cp_file_url'],
	 				'code_rate' => $code_type[$movie['code_rate']],
	 				'url' => basename($movie['cp_file_url'])
	 			]);
	 		}, $res);
	 		// dump($res);exit();
	 		return $res;
	 	}

	 // 	private function send_url($res)
		// {
		// 	$res = $this->download($res);
		// }

	 	private function get_movie_info($program_id,$code_type)
	 	{
	 		// dump($code_type);exit();
	 		$map['a.program_id'] = $program_id;
	 		// $map['a.code_rate'] = $code_type;
	 		$map['a.status'] = 0;
	 		$movie_info = D("Movie")->alias('a')->where($map)->join('join t_program as b on a.program_id = b.id')->field("a.id,cp_file_url,filesize,program_name,code_rate")->select();
	 		return $movie_info;
	 	}

	 	// private function download($res){
	 	// 	$refererUrl = parse_url($_SERVER['HTTP_REFERER']);
			// if(!isset($_SERVER['HTTP_REFERER']) || '47.98.244.226:8090/html/juji.php' != $refererUrl['host'].':'.$refererUrl['port'].$refererUrl['path']) {
			// 	exit('An error is occured!');
			// }
	 	// 	$url = $res['cp_file_url'];
	  //       $filesize = $res['filesize'];//文件大小
	  //       $code_type = C('CODERATE');
	  //       $code_rite = $code_type[$res['code_rate']];
	  //       $name = $res['program_name'];
	  //       $filename = $code_rite . '_' . $name . '.ts';
	 	// 	$ftp = "ftp://tianjin_iptv:7IJ4pkqlRC@47.98.244.226:2121/";
	 	// 	$file_name = $ftp.$url;
	  //       if(!is_file($file_name))//检测文件是否存在
	  //           exit('File is not exist');
	  //       exit;
	  //   }
	}